import { createStore } from 'vuex'
import {getLyric} from '@/api/index.js'
import dayjs from 'dayjs'
dayjs().format()
export default createStore({
  state: {
    playlist:[{
      name: "彩券",
      id: 1486513704,
      //总时长
      dt:"00:00",
      al:{
        id: 96680121,
        name: "彩券",
        pic: 109951165386012140,
        picUrl: "http://p3.music.126.net/NP1Zg57UUbHNzAhw7n-LeA==/109951165386012146.jpg",
        pic_str: "109951165386012146"
      }}],
    //歌曲列表的索引值
    playCurrentIndex:0,
    //歌词
    lyric:'',
    //歌曲进度时长
    currentTime:0,
    //歌曲初始时长
    // durationTime:"00:00",
    intervalId:0,
    //是否下方控件，默认不展示
    isShow:false,
    //是否播放歌曲
    isPlay:false,
  },
  getters:{
    lyricList(state){
          let arr = state.lyric.split(/\n/igs).map((item,i,arr)=>{
          let min = parseInt(item.slice(1,3)) ;
          let sec = parseInt(item.slice(4,6)) ;
          let mill = parseInt(item.slice(7,10));    
         return {
          min,sec,mill,
          lyric:item.slice(12,item.length),
          content:item,
          time:mill+sec*1000+min*60*1000
        }
      })
      
      arr.forEach((item,i)=>{
        if(i==0){
          item.pre = 0;
        }else{
          item.pre = arr[i-1].time
        }
        
      })
      return arr
  }
  },
  mutations: {
    // 歌单列表
    setPlaylist(state,value){
      state.playlist = value
    },
    pushPlaylist(state,value){
      state.playlist.push(value)
    },
    // 修改歌曲索引
    setPlayIndex(state,value){
      state.playCurrentIndex = value
    },
    // 修改歌词
    setLyric(state,value){
      state.lyric = value
    },
    // 修改时长
    setCurrentTime(state,value){
      state.currentTime = value
    },
    //修改歌曲总时长
    setSumTime(state,value){      
      value=dayjs(parseInt(value)).format('mm:ss')  
      state.playlist.dt = value
      // console.log('sunTime', state.playlist.dt)
    },
    //是否展示下方控件
    setShow(state,value){      
      state.isShow = value
    },
    //播放歌曲
    setIsPlay(state,value){
      state.isPlay=value
    }
  },
  actions: {
    async reqLyric(content,payload){
      let result = await getLyric(payload.id)
      console.log('这是歌词')
      content.commit('setLyric',result.data.lrc.lyric)
    },
  },
  modules: {
  }
})


